let myAudio;
const musicList = ["sound/bailongma.mp3", "sound/lanjingling.mp3"]; //把需要播放的歌曲从后往前排，这里已添加两首音乐，可继续添加多个音乐
let currMusicIdx = 0;
let inputing = false;
let is_leave = true;
const localStorage = window.localStorage;

function initMusicPlayer() {
	myAudio = document.getElementById("audioDom");
	if (!myAudio) {
		myAudio = new Audio();
		myAudio.id = 'audioDom';
		document.getElementById("music").appendChild(myAudio);
	}
	myAudio.preload = true;
	myAudio.controls = true;
	myAudio.loop = false; //禁止循环，否则无法触发ended事件
	myAudio.addEventListener('ended', musicPlayControl, false);
}

function musicPlayControl() {
	if (!myAudio || musicList.length == 0) {
		return;
	}
	if (currMusicIdx >= musicList.length) {
		currMusicIdx = 0;
	}
	myAudio.src = musicList[currMusicIdx++];
	myAudio.play();
}

function toSearch(v) {
	let keyword = typeof v === "string" ? v : document.getElementById("searchText").value;
	if (!keyword) {
		// return;
		keyword = '';
	} else {
		saveHistory(keyword);
	}
	document.getElementById("searchText").value = "";
	// return;
	hideHistory(1);
	let url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(keyword);
	window.open(url);
}

function doSomething (event) {
	// console.log(event);
	const key = event.key; // 获取按下的键名
	//console.log('按下了: ' + key);
	// 检查是否按下了特定的键，例如 'a'
	if (key === 'Enter' || key === 'enter' || key === 'ENTER') {
		// console.log('你按下了 Enter 键！');
		toSearch();
	}
}

function initBind() {
	searchButton.addEventListener("click", toSearch);

	document.getElementById('searchText').addEventListener('compositionstart', function (e) {
		inputing = true;
	}, false);

	document.getElementById('searchText').addEventListener('keypress', function (e) {
		if (!inputing) {
			doSomething(e);
		}
	}, false);

	document.getElementById('searchText').addEventListener('compositionend', function (e) {
		inputing = false;
	}, false);

	document.addEventListener('keypress', function (event) {
		const key = event.key; // 获取按下的键名
		//console.log('按下了: ' + key);
		// 检查是否按下了特定的键，例如 'a'
		if (key === 'Enter' || key === 'enter' || key === 'ENTER') {
			//console.log('你按下了 Enter 键！');
			if (!document.activeElement.isEqualNode(searchText)) {
				searchText.focus();
			}
		}
	});

	window.addEventListener('resize', function (e) {
		console.log('屏幕宽度:', window.innerWidth);
		console.log('屏幕高度:', window.innerHeight);
	}, false);
}
function showHistory() {
	let history = JSON.parse(localStorage.getItem("searchHistory"));
	if (!history) {
		history = [];
	}
	if (history.length == 0) {
		return;
	}
	let html = '';
	for (let i = history.length - 1; i >= 0; i--) {
		html += "<li onclick='useHistory(this.innerHTML);'>" + history[i] + "</li>";
	}
	historyList.innerHTML = html;
	historyList.scrollIntoView();
	searchHistoryArea.style.display = 'block';
	searchTextAreaHistory.style.borderRadius = "10px 10px 0 0";
	searchButton.style.borderRadius = "0 10px 0 0";
}

function hideHistory(force) {
	if (is_leave || force) {
		searchHistoryArea.style.display = 'none';
		searchTextAreaHistory.style.borderRadius = "10px 10px 10px 10px";
		searchButton.style.borderRadius = "0 10px 10px 0";
		historyList.innerHTML = '';
	} else {
		searchText.focus();
	}
}

function saveHistory(v) {
	let history = removeHistory(JSON.parse(localStorage.getItem("searchHistory")), v);
	history.push(v);
	if (history.length > 100) {
		history.splice(0, history.length - 100);
	}
	localStorage.setItem("searchHistory", JSON.stringify(history));
	showHistory();
}

function removeHistory(history, v) {
	if (!history) {
		history = [];
	}
	for (let i = history.length - 1; i >= 0; i--) {
		if (history[i] == v) {
			history.splice(i, 1);
		}
	}
	return history;
}

function clearHistory() {
	localStorage.removeItem("searchHistory");
	console.log('clear history');
	hideHistory(true);
}

function useHistory(v) {
	searchText.value = v;
	console.log('use history');
	searchButton.click();
}

window.onload = function() {
	initMusicPlayer();
	musicPlayControl();
	initBind();
}